<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false"
               :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate == 'add' ? '添加' : '编辑' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
<!--                    <FormItem-->
<!--                        type="remoteSelect"-->
<!--                        prop="pid"-->
<!--                        label="上级"-->
<!--                        v-model.number="baTable.form.items!.pid"-->
<!--                        placeholder="默认为顶级"-->
<!--                        :input-attr="{-->
<!--                            params: { isTree: true },-->
<!--                            field: 'name',-->
<!--                            'remote-url': '/auth/tissue/select',-->
<!--                        }"-->
<!--                    />-->
					<el-form-item label="上级菜单规则">
						<el-tree-select
							v-model="baTable.form.items!.pid"
							:data="select"
							@click="getSelect"
							placeholder="默认顶级"
							check-strictly
							:render-after-expand="false"
							style="width: 240px"
						/>
					</el-form-item>

                    <el-form-item prop="name" label="名称">
                        <el-input
                            v-model="baTable.form.items!.name"
                            type="string"
                            placeholder="请输入名称"
                        ></el-input>
                    </el-form-item>
                    <!--                    <el-form-item prop="name" label="组织代码">-->
                    <!--                        <el-input-->
                    <!--                            v-model="baTable.form.items!.code"-->
                    <!--                            type="string"-->
                    <!--                            placeholder="请输入名称"-->
                    <!--                        ></el-input>-->
                    <!--                    </el-form-item>-->

                    <FormItem
                        label="状态"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :data="{
                            content: { '0': '禁用', '1': '启用' },
                            childrenAttr: { border: true },
                        }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">取消</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                           type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? '保存并编辑下一项' : '保存' }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import {reactive, ref, inject, onMounted, watch} from 'vue'
import type baTableClass from '/src/utils/baTable'
import FormItem from '/src/components/formItem/index.vue'
import {buildValidatorData} from '/src/utils/validate'
import type {ElForm, FormItemRule} from 'element-plus'
import createAxios from "/@/utils/axios";
import {uuid} from "/@/utils/random";

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    name: [buildValidatorData({name: 'required', title: '名称'})],
    code: [buildValidatorData({name: 'required', title: '组织代码'})],
    pid: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val) {
                    return callback()
                }
                if (parseInt(val) == parseInt(baTable.form.items!.id)) {
                    return callback(new Error('上级不能是本身'))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
})

const select = ref([{
	label: "顶级",
	value: 0,
}])

const getSelect = () => {
	select.value = [{
		label: "顶级",
		value: 0,
	}]
	createAxios({
		url: '/admin_api/area/get_select',
		method: 'get',
	}).then((res) => {
		select.value = select.value.concat(res.data)

	})
}
watch(
	() => baTable.form.items!.pid,
	() => {
		getSelect()
	}
)
</script>

<style scoped lang="scss"></style>
